/**
*
* AndroidMenuItem.as
* Niels Overwijn
* 
* Copyright (c) 2010 Niels Overwijn
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package niels.android
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	/**
	 * Menu item, button
	 * @author Niels Overwijn
	 */
	public class AndroidMenuItem extends Sprite
	{
		
		private var _background:Sprite;
		private var _backgroundColor:uint;
		private var _label:TextField;
		private var _labelText:String;
		private var _menuItemWidth:Number;
		private var _menuItemHeight:Number;
		private var _defaultHandler:Function;
		private var _icon:Bitmap;
		private var _iconD:BitmapData;
		
		/**
		 * Constructor
		 * @param label Text that will be shown on the menu item
		 * @param defaultHandler Function that will be called on click
		 * @param icon Icon that will be shown instead of the text
		 */
		public function AndroidMenuItem(label:String, defaultHandler:Function = null, icon:DisplayObject = null) 
		{
			
			_menuItemWidth = AndroidConfig.MENU_WIDTH/3;
			_menuItemHeight = AndroidConfig.MENUITEM_HEIGHT;
			_backgroundColor = AndroidConfig.MENUITEM_BACKGROUND_COLOR;
			_labelText = label;
			_background = new Sprite();
			addChild(_background);
			
			setUpBackground();
			
			if (icon == null)
			{
				_label = new TextField();
				addChild(_label);
				setUpLabel();
			} else
			{
				_iconD = new BitmapData(icon.width, icon.height, true);
				_icon = new Bitmap(_iconD);
				_iconD.draw(icon);
				addChild(_icon);
				setUpIcon();
			}
			
			_defaultHandler = defaultHandler;
			this.addEventListener(MouseEvent.CLICK, this_clickHandler);
			this.addEventListener(MouseEvent.MOUSE_OVER, this_mouseOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, this_mouseOutHandler);
		}
		
		///////////////////////
		// PRIVATE FUNCTIONS //
		///////////////////////
		
		/**
		 * Sets the menu item to the original background color
		 * @param	e
		 */
		private function this_mouseOutHandler(e:MouseEvent):void 
		{
			_backgroundColor = AndroidConfig.MENUITEM_BACKGROUND_COLOR;
			setUpBackground();
		}
		
		/**
		 * Sets the menu item to the "over" state color
		 * @param	e
		 */
		private function this_mouseOverHandler(e:MouseEvent):void 
		{
			_backgroundColor = AndroidConfig.MENUITEM_BACKGROUND_COLOR_OVER;
			setUpBackground();
		}
		
		/**
		 * Handles the click on the menu item
		 * @param	e
		 */
		private function this_clickHandler(e:MouseEvent):void 
		{
			if (_defaultHandler != null)
			{
				_defaultHandler(e);
			} 
			if (AndroidConfig.AUTO_CLOSE_AFTER_SELECT)
			{
				dispatchEvent(new Event(Event.CLOSE));
			}
		}
		
		/**
		 * Sets up the menu background
		 */
		private function setUpBackground():void
		{
			_background.graphics.clear();
			_background.graphics.lineStyle(AndroidConfig.MENUITEM_BORDER_WIDTH, AndroidConfig.MENUITEM_BORDER_COLOR, AndroidConfig.MENUITEM_BORDER_ALPHA);
			_background.graphics.beginFill(_backgroundColor, AndroidConfig.MENUITEM_BACKGROUND_ALPHA);
			_background.graphics.drawRect(0, 0, _menuItemWidth, _menuItemHeight);
			_background.graphics.endFill();
			
		}
		
		/**
		 * Sets up the label shown on the menu item
		 */
		private function setUpLabel():void
		{
			try
			{
				_label.width = _menuItemWidth;
				_label.height = AndroidConfig.MENUITEM_TEXTFORMAT.size + 6;
				_label.text = _labelText;
				_label.selectable = false;
				_label.defaultTextFormat = AndroidConfig.MENUITEM_TEXTFORMAT;
				_label.y = (_menuItemHeight / 2) - (_label.height / 2);
			} catch (e:Error)
			{
				setUpIcon();
			}
			
		}
		
		/**
		 * positions the icon to the center of the menu item
		 */
		private function setUpIcon():void
		{
			_icon.y = (_menuItemHeight / 2) - (_icon.height / 2);
			_icon.x = (_menuItemWidth / 2) - (_icon.width / 2);
		}
		
		///////////////
		// OVERRIDES //
		///////////////
		
		/**
		 * Returns the width of the menu item
		 */
		public override function get width():Number { return super.width; }
		
		/**
		 * Sets the width of the menu item, redraws the background and reposition the label/icon
		 */
		public override function set width(value:Number):void 
		{
			_menuItemWidth = value;
			setUpBackground();
			setUpLabel();
		}
		
		/**
		 * Returns the height of the menu item
		 */
		public override function get height():Number { return super.height; }
		
		/**
		 * Sets the height of the menu item, redraws the background and reposition the label/icon
		 */
		public override function set height(value:Number):void 
		{
			_menuItemHeight = value;
			setUpBackground();
			setUpLabel();
		}
		
	}

}